package wscfg

import (
	"encoding/json"
	"io/ioutil"
	"log"
)

type NetIpListenAddr struct {
	Addr       string `json:"addr"`
	IpMaxCount int    `json:"ip_max_count"`
	MaxPkgSize int    `json:"maxpkgsize"`
}

type MatchAddr struct {
	Addr             string `json:"addr"`
	HeartBeatTimeout int    `json:"heartbeattimeout"`
	MaxPkgSize       int    `json:"maxpkgsize"`
}

type MarketpriceAddr struct {
	Addr             string `json:"addr"`
	HeartBeatTimeout int    `json:"heartbeattimeout"`
	MaxPkgSize       int    `json:"maxpkgsize"`
}

type GlobserverAddr struct {
	Addr             string `json:"addr"`
	HeartBeatTimeout int    `json:"heartbeattimeout"`
	MaxPkgSize       int    `json:"maxpkgsize"`
}

type PenserverAddr struct {
	Addr             string `json:"addr"`
	HeartBeatTimeout int    `json:"heartbeattimeout"`
	MaxPkgSize       int    `json:"maxpkgsize"`
}

type OrdersAddr struct {
	Host      []string `json:"host"`
	Topic     string   `json:"topic"`
	Partition int      `json:"partition"`
}
type PlanordersAddr struct {
	Host      []string `json:"host"`
	Topic     string   `json:"topic"`
	Partition int      `json:"partition"`
}
type Balances struct {
	Host      []string `json:"host"`
	Topic     string   `json:"topic"`
	Partition int      `json:"partition"`
}
type Dealresult struct {
	Host      []string `json:"host"`
	Topic     string   `json:"topic"`
	Partition int      `json:"partition"`
}
type FilterLimit struct {
	On                   int `json:"on"`
	Countlimit           int `json:"countlimit"`
	Cmdlimit             int `json:"cmdlimit"`
	CountStopServiceTime int `json:"count_stop_service_time"`
	Errorlimit           int `json:"errorlimit"`
	ErrorStopServiceTime int `json:"error_stop_service_time"`
	ClearCacheInterval   int `json:"clear_cache_interval"`
}

type KafkaSet struct {
	Redisaddr string `json:"redisaddr"`
	Rediskey  string `json:"rediskey"`
	Isredis   int    `json:"isredis"`
	Cfgpath   string `json:"cfgpath"`
}

type Check struct {
	RedisAddr         string   `json:"redisAddr"`
	MysqlAddr         string   `json:"mysqlAddr"`
	MysqlUser         string   `json:"mysqlUser"`
	MysqlPsw          string   `json:"mysqlPsw"`
	Mysqldb           string   `json:"mysqldb"`
	LogProj           string   `json:"logproj"`
	Log_logStore      string   `json:"log_log_store"`
	Log_endPoint      string   `json:"log_end_point"`
	Log_access_key_id string   `json:"log_access_key_id"`
	Log_access_secert string   `json:"log_access_secert"`
	Log_topic         string   `json:"log_topic"`
	Log_enable        int      `json:"log_enable"`
	Stage             string   `json:"stage"`
	WhiteIps          []string `json:"whiteips"`
}

type myConf struct {
	SeverCfg     NetIpListenAddr   `json:"SeverCfg"`
	Ssid         int               `json:"ssid"`
	Ispublic     int               `json:"ispublic"`
	Matchengine  MatchAddr         `json:"matchengine"`
	Marketprice  []MarketpriceAddr `json:"marketprice"`
	Globalserver GlobserverAddr    `json:"globalserver"`
	Penserver    PenserverAddr     `json:"penserver"`
	Orders       OrdersAddr        `json:"orders"`
	Planorders   PlanordersAddr    `json:"planorders"`
	Balances     Balances          `json:"balances"`
	Dealresult   Dealresult        `json:"dealresult"`
	Filter       FilterLimit       `json:"filter"`
	Kafkaset     KafkaSet          `json:"kafkaset"`
	Check        Check             `json:"check"`
	Etcd         []string          `json:"etcd"`
	Cachetimeout int               `json:"cachetimeout"`
	Auth_url     string            `json:"auth_url"`
	Sign_url     string            `json:"sign_url"`
	Logerxml     string            `json:"logerxml"`
	Depth_limit  []int             `json:"depth_limit"`
	Depth_merge  []string          `json:"depth_merge"`
}

func ReadConf(filename string) (myConf, bool) {
	var wsconf myConf
	content, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Panicln(err)
		return wsconf, false
	}

	if err = json.Unmarshal([]byte(content), &wsconf); err != nil {
		log.Panicln("unmarshal conf error")
		return wsconf, false
	}
	return wsconf, true
}
